< Summary

Class:GDX.Reflection
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Reflection.cs
Covered lines:119
Uncovered lines:0
Coverable lines:119
Total lines:290
Line coverage:100% (119 of 119)
Covered branches:0
Total branches:0
Covered methods:11
Total methods:11
Method coverage:100% (11 of 11)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Reflection()0%110100%
GetDefault(...)0%3.043083.33%
GetType(...)0%33092.86%
InvokeStaticMethod(...)0%4.14081.82%
InvokeMethod(...)0%440100%
SetFieldOrPropertyValue(...)0%4.494068.75%
SetFieldValue(...)0%3.053081.82%
SetPropertyValue(...)0%3.053081.82%
TryGetFieldValue[T](...)0%3.513061.54%
TryGetFieldOrPropertyValue(...)0%5.194057.89%
TryGetPropertyValue[T](...)0%3.513061.54%

File(s)

./Packages/com.dotbunny.gdx/GDX/Reflection.cs

#LineLine coverage
 1// Copyright (c) 2020-2023 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System;
 6using System.Reflection;
 7
 8namespace GDX
 9{
 10    /// <summary>
 11    ///     A collection of reflection related helper utilities.
 12    /// </summary>
 13    /// <remarks>Torn about the existence of this utility class, yet alone the conditions dictating it.</remarks>
 14    public static class Reflection
 15    {
 16        /// <summary>
 17        ///     <see cref="BindingFlags"/> for a private field.
 18        /// </summary>
 19        public const BindingFlags PrivateFieldFlags = BindingFlags.Instance | BindingFlags.NonPublic;
 20        /// <summary>
 21        ///     <see cref="BindingFlags"/> for a private static.
 22        /// </summary>
 23        public const BindingFlags PrivateStaticFlags = BindingFlags.Static | BindingFlags.NonPublic;
 24        /// <summary>
 25        ///     <see cref="BindingFlags"/> for a public static.
 26        /// </summary>
 27        public const BindingFlags PublicStaticFlags = BindingFlags.Static | BindingFlags.Public;
 28
 29        /// <summary>
 30        ///     The assembly qualified name for <see cref="UnityEngine.Object" />
 31        /// </summary>
 232        public static readonly string UnityObjectName = typeof(UnityEngine.Object).AssemblyQualifiedName;
 33
 34        /// <summary>
 35        ///     The assembly qualified name for <see cref="Serializable.SerializableTypes" />
 36        /// </summary>
 237        public static readonly string SerializedTypesName =
 38            typeof(Serializable.SerializableTypes).AssemblyQualifiedName;
 39
 40        /// <summary>
 41        ///     Returns the default value for a given type.
 42        /// </summary>
 43        /// <param name="type">A qualified type.</param>
 44        /// <returns>The default value.</returns>
 45        public static object GetDefault(this Type type)
 246        {
 247            if (type.IsClass || !type.IsValueType)
 148            {
 149                return null;
 50            }
 151            return Activator.CreateInstance(type);
 252        }
 53
 54        /// <summary>
 55        ///     Returns a qualified type..
 56        /// </summary>
 57        /// <param name="type">The full name of a type.</param>
 58        /// <returns>A <see cref="System.Type"/> if found.</returns>
 59        public static Type GetType(string type)
 2960        {
 2961            Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
 2962            int loadedAssembliesCount = loadedAssemblies.Length;
 512663            for (int i = 0; i < loadedAssembliesCount; i++)
 256164            {
 256165                Type targetType = loadedAssemblies[i].GetType(type);
 256166                if (targetType != null)
 2767                {
 2768                    return targetType;
 69                }
 253470            }
 271            return null;
 2972        }
 73
 74        /// <summary>
 75        ///     Invokes a known static method.
 76        /// </summary>
 77        /// <param name="type">The explicit type of the static class.</param>
 78        /// <param name="method">The name of the method to invoke.</param>
 79        /// <param name="parameters">Any parameters that should be passed to the method?</param>
 80        /// <param name="flags">The <paramref name="method"/>'s access flags.</param>
 81        /// <returns>An <see cref="object"/> of the return value. This can be null.</returns>
 82        public static object InvokeStaticMethod(string type, string method, object[] parameters = null,
 83            BindingFlags flags = PublicStaticFlags)
 684        {
 685            Type targetType = GetType(type);
 686            if (targetType != null)
 587            {
 588                MethodInfo targetMethod = targetType.GetMethod(method, flags);
 589                if (targetMethod != null)
 490                {
 491                    return targetMethod.Invoke(null, parameters ?? Core.EmptyObjectArray);
 92                }
 193            }
 294            return null;
 695        }
 96
 97        /// <summary>
 98        ///     Invoke a known private method on an object.
 99        /// </summary>
 100        /// <param name="targetObject">The ambiguous object to invoke a method on.</param>
 101        /// <param name="method">The name of the method to invoke.</param>
 102        /// <param name="parameters">Any parameters that should be passed to the method?</param>
 103        /// <param name="flags">The <paramref name="method"/>'s access flags.</param>
 104        /// <returns>An <see cref="object"/> of the return value. This can be null.</returns>
 105        public static object InvokeMethod(object targetObject, string method, object[] parameters = null,
 106            BindingFlags flags = PrivateFieldFlags)
 11107        {
 11108            Type targetType = targetObject.GetType();
 11109            MethodInfo targetMethod = targetType.GetMethod(method, flags);
 11110            return targetMethod != null ? targetMethod.Invoke(targetObject, parameters ?? Core.EmptyObjectArray) : null;
 11111        }
 112
 113        /// <summary>
 114        ///     Set the field or property value of a specific <paramref name="targetObject"/>, which may not be
 115        ///     normally accessible.
 116        /// </summary>
 117        /// <param name="targetObject">The instanced object which will have it's field or property value set.</param>
 118        /// <param name="name">The field or property's name to set.</param>
 119        /// <param name="value">The value to set the field or property to.</param>
 120        /// <param name="fieldFlags">The field's access flags.</param>
 121        /// <param name="propertyFlags">The property's access flags.</param>
 122        /// <returns>true/false if the value was set.</returns>
 123        public static bool SetFieldOrPropertyValue(object targetObject, string name, object value,
 124            BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags)
 6125        {
 6126            if (targetObject == null)
 1127                return false;
 128
 5129            Type type = targetObject.GetType();
 5130            FieldInfo f = type.GetField(name, fieldFlags);
 5131            if (f != null)
 2132            {
 2133                f.SetValue(targetObject, value);
 2134                return true;
 135            }
 136
 3137            PropertyInfo p = type.GetProperty(name,propertyFlags);
 3138            if (p != null)
 2139            {
 2140                p.SetValue(targetObject, value);
 2141                return true;
 142            }
 143
 1144            return false;
 6145        }
 146
 147        /// <summary>
 148        ///     Set the field value of a specific <paramref name="targetObject"/>, which may not be normally accessible.
 149        /// </summary>
 150        /// <param name="targetObject">The instanced object which will have it's field value set; use a null value if th
 151        /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param>
 152        /// <param name="name">The field's name to set.</param>
 153        /// <param name="value">The value to set the field to.</param>
 154        /// <param name="flags">The field's access flags.</param>
 155        /// <returns>true/false if the value was set.</returns>
 156        public static bool SetFieldValue(object targetObject, Type type, string name, object value,
 157            BindingFlags flags = PrivateFieldFlags)
 6158        {
 6159            if (type != null)
 5160            {
 5161                FieldInfo field = type.GetField(name, flags);
 5162                if (field != null)
 4163                {
 4164                    field.SetValue(targetObject, value);
 4165                    return true;
 166                }
 1167            }
 2168            return false;
 6169        }
 170
 171        /// <summary>
 172        ///     Set the property value of a specific <paramref name="targetObject"/>, which may not be normally accessib
 173        /// </summary>
 174        /// <param name="targetObject">The instanced object which will have it's property value set; use a null value if
 175        /// <param name="type">The type of the <paramref name="targetObject"/>.</param>
 176        /// <param name="name">The property's name to set.</param>
 177        /// <param name="value">The value to set the property to.</param>
 178        /// <param name="flags">The property's access flags.</param>
 179        /// <returns>true/false if the value was set.</returns>
 180        public static bool SetPropertyValue(object targetObject, Type type, string name, object value,
 181            BindingFlags flags = PrivateFieldFlags)
 4182        {
 4183            if (type != null)
 3184            {
 3185                PropertyInfo property = type.GetProperty(name, flags);
 3186                if (property != null)
 2187                {
 2188                    property.SetValue(targetObject, value);
 2189                    return true;
 190                }
 1191            }
 2192            return false;
 4193        }
 194
 195        /// <summary>
 196        ///     Try to access the field value of a specific <paramref name="targetObject"/>, which may not be normally a
 197        /// </summary>
 198        /// <remarks></remarks>
 199        /// <param name="targetObject">The instanced object which will have it's field value read; use a null value if t
 200        /// <param name="type">The qualified type of the <paramref name="targetObject"/>.</param>
 201        /// <param name="name">The field's name to read.</param>
 202        /// <param name="returnValue">The returned value from the field, the default value if the field was unable to be
 203        /// <param name="flags">The field's access flags.</param>
 204        /// <typeparam name="T">The type of data being read from the field.</typeparam>
 205        /// <returns>true/false if the process was successful.</returns>
 206        public static bool TryGetFieldValue<T>(object targetObject, Type type, string name, out T returnValue, BindingFl
 7207        {
 7208            if (type == null)
 1209            {
 1210                returnValue = default;
 1211                return false;
 212            }
 6213            FieldInfo fieldInfo = type.GetField(name, flags);
 6214            if (fieldInfo == null)
 1215            {
 1216                returnValue = default;
 1217                return false;
 218            }
 5219            returnValue = (T)fieldInfo.GetValue(targetObject);
 5220            return true;
 7221        }
 222
 223        /// <summary>
 224        ///     Try to access the field or property value of a specific <paramref name="targetObject"/>, which may not
 225        ///     be normally accessible.
 226        /// </summary>
 227        /// <remarks>Useful for when you really do not know the <see cref="System.Type"/>.</remarks>
 228        /// <param name="targetObject">The instanced object which will have it's field or property value read.</param>
 229        /// <param name="name">The field or property's name to read.</param>
 230        /// <param name="returnValue">The returned value from the field or property, the default value if the property w
 231        /// <param name="fieldFlags">The field's access flags.</param>
 232        /// <param name="propertyFlags">The property's access flags.</param>
 233        /// <returns>true/false if a value was found.</returns>
 234        public static bool TryGetFieldOrPropertyValue(object targetObject, string name, out object returnValue,
 235            BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags)
 7236        {
 7237            if (targetObject == null)
 1238            {
 1239                returnValue = null;
 1240                return false;
 241            }
 242
 6243            Type type = targetObject.GetType();
 6244            FieldInfo f = type.GetField(name, fieldFlags);
 6245            if (f != null)
 2246            {
 2247                returnValue = f.GetValue(targetObject);
 2248                return true;
 249            }
 250
 4251            PropertyInfo p = type.GetProperty(name,propertyFlags);
 4252            if (p != null)
 3253            {
 3254                returnValue = p.GetValue(targetObject);
 3255                return true;
 256            }
 257
 1258            returnValue = default;
 1259            return false;
 7260        }
 261
 262        /// <summary>
 263        ///     Try to get a property value from <paramref name="targetObject"/>, which may not be normally accessible.
 264        /// </summary>
 265        /// <param name="targetObject">The instanced object which will have it's property value read; use a null value i
 266        /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param>
 267        /// <param name="name">The property's name to read.</param>
 268        /// <param name="returnValue">The returned value from the property, the default value if the property was unable
 269        /// <param name="flags">The property's access flags.</param>
 270        /// <typeparam name="T">The type of data being read from the property.</typeparam>
 271        /// <returns>true/false if the process was successful.</returns>
 272        public static bool TryGetPropertyValue<T>(object targetObject, Type type, string name, out T returnValue, Bindin
 5273        {
 5274            if (type == null)
 1275            {
 1276                returnValue = default;
 1277                return false;
 278            }
 4279            PropertyInfo propertyInfo = type.GetProperty(name, flags);
 4280            if (propertyInfo == null)
 1281            {
 1282                returnValue = default;
 1283                return false;
 284            }
 285
 3286            returnValue = (T)propertyInfo.GetValue(targetObject);
 3287            return true;
 5288        }
 289    }
 290}

Coverage by test methods































































































































































































































































































































































































































































































































































































































































































Methods/Properties

Reflection()
GetDefault(System.Type)
GetType(System.String)
InvokeStaticMethod(System.String, System.String, System.Object[], System.Reflection.BindingFlags)
InvokeMethod(System.Object, System.String, System.Object[], System.Reflection.BindingFlags)
SetFieldOrPropertyValue(System.Object, System.String, System.Object, System.Reflection.BindingFlags, System.Reflection.BindingFlags)
SetFieldValue(System.Object, System.Type, System.String, System.Object, System.Reflection.BindingFlags)
SetPropertyValue(System.Object, System.Type, System.String, System.Object, System.Reflection.BindingFlags)
TryGetFieldValue[T](System.Object, System.Type, System.String, , System.Reflection.BindingFlags)
TryGetFieldOrPropertyValue(System.Object, System.String, System.Object&, System.Reflection.BindingFlags, System.Reflection.BindingFlags)
TryGetPropertyValue[T](System.Object, System.Type, System.String, , System.Reflection.BindingFlags)